//初始化数据库引用
const db = wx.cloud.database()
const home = db.collection('HH_HomeWorldReward')
const person = db.collection('HH_TeacherVolstudent')
const majorProject = db.collection('HH_ProjectMajor')
const group = db.collection('HH_CompanyGroup')
const _ = db.command
Page({
  data: {
    type: "",
    school: [],
    world: [],
    reward: [],
    teacher: [],
    volStudent: [],
    plan: [],
    major: [],
    company: [],
    group: [],
    item: 0,
    content_tab: 0,
    keyFiled: "",
    pageContent: [],
    currentType: "",
    search_mark: [],
  },
  //下拉触底刷新scroll-viwe
  scrolltolower: function (e) {
    var oldPage = 0
    var oldData = []
    var type = this.data.type
    var scrollbase = null
    //判断传入的那个页面
    if (type == "school") {
      oldData = this.data.school
      scrollbase = home
    } else if (type == "reward") {
      oldData = this.data.reward
      scrollbase = home
    } else if (type == "world") {
      oldData = this.data.world
      scrollbase = home
    } else if (type == "plan") {
      oldData = this.data.plan
      scrollbase = majorProject
    } else if (type == "teacher") {
      oldData = this.data.teacher
      scrollbase = person
    } else if (type == "volStudent") {
      oldData = this.data.volStudent
      scrollbase = person
    } else if (type == "major") {
      oldData = this.data.majorProject
      scrollbase = home
    } else if (type == "company") {
      oldData = this.data.company
      scrollbase = group
    } else if (type == "group") {
      oldData = this.data.group
      scrollbase = group
    }
    //循环当前页面已经存在的东西
    for (let index = 0; index < oldData.length; index++) {
      oldPage++
    }
    //去请求数据库，拿到剩下的东西
    scrollbase.where({
      mark: _.eq(this.data.type)
    }).skip(oldPage).get().then(res => {
      this.setData({
        [type]: oldData.concat(res.data)
      })
    })
  },
  onLoad: function (options) {
    this.setData({
      type: options.type
    })
    //判断点击页面传进来的参数，以此来判定渲染是哪个页面
    if (this.data.type == "school") {
      home.where({
        mark: _.eq(this.data.type)
      }).get().then(res => {
        this.setData({
          school: res.data
        })
      })
    }
    if (this.data.type == "world") {
      home.where({
        mark: _.eq(this.data.type)
      }).get().then(res => {
        this.setData({
          world: res.data
        })
      })
    }
    if (this.data.type == "reward") {
      home.where({
        mark: _.eq(this.data.type)
      }).get().then(res => {
        this.setData({
          reward: res.data
        })
      })
    }
    if (this.data.type == "teacher") {
      person.where({
        mark: _.eq(this.data.type)
      }).get().then(res => {
        this.setData({
          teacher: res.data
        })
      })
    }
    if (this.data.type == "volStudent") {
      person.where({
        mark: _.eq(this.data.type)
      }).get().then(res => {
        this.setData({
          volStudent: res.data
        })
      })
    }
    if (this.data.type == "plan") {
      majorProject.where({
        mark: _.eq(this.data.type)
      }).get().then(res => {
        this.setData({
          plan: res.data
        })

      })
    }
    if (this.data.type == "major") {
      majorProject.where({
        mark: _.eq(this.data.type)
      }).get().then(res => {
        this.setData({
          major: res.data
        })
      })
    }
    if (this.data.type == "company") {

      group.where({
        mark: _.eq(this.data.type)
      }).get().then(res => {
        this.setData({
          company: res.data
        })
      })
    }
    if (this.data.type == "group") {
      group.where({
        mark: _.eq(this.data.type)
      }).get().then(res => {
        this.setData({
          group: res.data
        })
      })
    }
  },
  //抽屉出柜特效
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  //抽屉入柜特效
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  //绑定swiper点哪指哪切换
  tabchangeitem: function (e) {
    this.setData({
      item: e.currentTarget.dataset.item
    })
    this.hideModal()
  },
  //绑定swiper-item拖哪指哪切换
  contentchangeitem: function (e) {
    this.setData({
      content_tab: e.detail.current
    })
  },
  //获取搜索框的值暂时停摆，日后迁移到homepage
  change: function (e) {
    this.setData({
      inputContent: e.detail.value
    })
  },
  //搜索
  search: function (e) {
    var dataType = this.data.type
    var searchContent = this.data.inputContent
    var searchbase = null
    //判断需要搜索的数据库
    if (dataType == "school" || dataType == "world" || dataType == "reward") {
      searchbase = home
    }
    if (dataType == "volStudent" || dataType == "teacher") {
      searchbase = person
    }
    if (dataType == "major" || dataType == "plan") {
      searchbase = majorProject
    }
    if (dataType == "company" || dataType == "group") {
      searchbase = group
    }
    //判断是否为空值搜索
    if (searchContent == null) {
      wx.showToast({
        title: '输入消息为空',
        image: '../../images/warning.png'
      })
    } else {
      //设定要拿到的类型数据，然后对相应字段进行正则查询
      searchbase.where(
        _.and([
          _.or({
            mark: _.eq(dataType)
          }),
          _.or({
            name: db.RegExp({
              regexp: searchContent
            }),
          }, {
            mainWord: db.RegExp({
              regexp: searchContent
            }),
          }, {
            keyWord: db.RegExp({
              regexp: searchContent
            }),
          }, {
            Fleader: db.RegExp({
              regexp: searchContent
            }),
          }, {
            Sleader: db.RegExp({
              regexp: searchContent
            }),
          }, {
            leadered_area: db.RegExp({
              regexp: searchContent
            }),
          }, {
            qq_group: db.RegExp({
              regexp: searchContent
            }),
          }, {
            type: db.RegExp({
              regexp: searchContent
            }),
          }, {
            area: db.RegExp({
              regexp: searchContent
            }),
          }, {
            paid: db.RegExp({
              regexp: searchContent
            }),
          }, {
            time: db.RegExp({
              regexp: searchContent
            }),
          }, {
            paid_area: db.RegExp({
              regexp: searchContent
            }),
          }, {
            reward_type: db.RegExp({
              regexp: searchContent
            }),
          }, {
            avg_line: db.RegExp({
              regexp: searchContent
            }),
          }, {
            get_line: db.RegExp({
              regexp: searchContent
            }),
          }, {
            pro_line: db.RegExp({
              regexp: searchContent
            }),
          }, {
            study_direction: db.RegExp({
              regexp: searchContent
            }),
          }, {
            study_year: db.RegExp({
              regexp: searchContent
            }),
          }, {
            age: db.RegExp({
              regexp: searchContent
            }),
          }, {
            class: db.RegExp({
              regexp: searchContent
            }),
          }, {
            magjor: db.RegExp({
              regexp: searchContent
            }),
          }, {
            rank: db.RegExp({
              regexp: searchContent
            }),
          }, {
            class_number: db.RegExp({
              regexp: searchContent
            }),
          }, {
            phone_number: db.RegExp({
              regexp: searchContent
            }),
          }, {
            phone_number: db.RegExp({
              regexp: searchContent
            }),
          }, {
            qq_number: db.RegExp({
              regexp: searchContent
            }),
          }, {
            wx_number: db.RegExp({
              regexp: searchContent
            }),
          })
        ])
      ).get().then(res => {
        //更新页面信息
        this.setData({
          [dataType]: res.data
        })
      })
    }
  }
})